package com.mtons.mblog.web.controller.learn;

import com.mtons.mblog.base.lang.EntityStatus;
import com.mtons.mblog.base.lang.Result;
import com.mtons.mblog.modules.data.LearnPlanSetVO;
import com.mtons.mblog.modules.service.LearnPlanSetService;
import com.mtons.mblog.web.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller("learnPlanController")
@RequestMapping("/learn/plan")
public class LearnPlanController extends BaseController {

    @Resource
    private LearnPlanSetService learnPlanSetService;

    @RequestMapping(value = {"/", "", "/index"})
    public String index(ModelMap modelMap) {
        final long userId = getProfile().getId();
        List<LearnPlanSetVO> planSetVOList = learnPlanSetService.queryPlanSettings(userId);
        modelMap.put("planSet", planSetVOList);
        return "learn/plan/list";
    }

    @RequestMapping("/status")
    @ResponseBody
    public Result status(String id, HttpServletRequest request) {
        Result data = Result.failure("操作失败");
        String status = ServletRequestUtils.getStringParameter(request, "status", EntityStatus.IS_VALID);
        if (id != null) {
            try {
                learnPlanSetService.updateStatus(id, status);
                data = Result.success();
            } catch (Exception e) {
                data = Result.failure(e.getMessage());
            }
        }
        return data;
    }
}
